package SlidingWindow;

/**
 * @ClassName LC904
 * @Description TODO
 * @Author ZJX
 * @Date 2024/9/20 13:26
 * @Version 1.0
 */
public class LC904 {
    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        int[] map = new int[12800];
        int maxLength = 0;
        int kind = 0;

        for (int right = 0, left = 0; right < fruits.length; right++) {
            if (map[fruits[right]] == 0){
                kind++;
            }
            map[fruits[right]]++;

            while(kind > 2){
                map[fruits[left]]--;
                if (map[fruits[left]] == 0){
                    kind--;
                }
                left++;
            }
            maxLength = Math.max(maxLength, right - left + 1);
        }
        return maxLength;
    }
}
